package com.yanqu.road.dao.impl.systemloop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.systemloop.SystemLoopData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSystemLoopDaoImpl extends TempDataObjectDao<SystemLoopData> {
    public static String getSystemLoopDataSql = "select * from `t_c_system_loop`";

    public Map<Integer, SystemLoopData> getSystemLoopData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, SystemLoopData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSystemLoopDataSql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                SystemLoopData data = parse(rs);
                dataMap.put(data.getLoopId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateSystemLoopDataSql = "update `t_c_system_loop` set lastWeekId=?,systemId=?,serverIdSet=? where loopId=?";
    @Override
    protected boolean updateData(SystemLoopData systemLoopData) {
        systemLoopData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, systemLoopData.getLastWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, systemLoopData.getSystemId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(systemLoopData.getServerIdSet())));
        para.put(i++, new DbParameter(Types.INTEGER, systemLoopData.getLoopId()));
        if (getDb().executeNoneQuery(updateSystemLoopDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSystemLoopDataSql = "insert into `t_c_system_loop`(loopId,lastWeekId,systemId,serverIdSet) values (?,?,?,?)";
    @Override
    protected boolean addData(SystemLoopData systemLoopData) {
        systemLoopData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, systemLoopData.getLoopId()));
        para.put(i++, new DbParameter(Types.INTEGER, systemLoopData.getLastWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, systemLoopData.getSystemId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(systemLoopData.getServerIdSet())));
        if (getDb().executeNoneQuery(insertSystemLoopDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SystemLoopData parseData(ResultSet rs) throws SQLException {
        SystemLoopData systemLoopData = new SystemLoopData();
        systemLoopData.setLoopId(rs.getInt("loopId"));
        systemLoopData.setLastWeekId(rs.getInt("lastWeekId"));
        systemLoopData.setSystemId(rs.getInt("systemId"));
        systemLoopData.setServerIdSet(JSON.parseObject(rs.getString("serverIdSet"), new TypeReference<HashSet<Long>>() {
        }));

        return systemLoopData;
    }
}
